/**
* Created by ren on 2017/1/3.
*/
<template>
  <div class='navCar'>
    <div class="container">
      <div :class="{'nav-wrap':true,disabled:_parentThis.isHide}" :data="_parentThis.carNum">
        <div :class="{'nav-car-wrap':true,realgold:true,disabled:_parentThis.realgoldStatus}">
          <div>即时金价(元/克)</div>
          <div style="font-size: 30px;color: #fd8f23">{{(goldPrice/100).toFixed(2)}}</div>
          <span class="badge" @click="closeRealGold">X</span>
        </div>
        <div :class="{'nav-car-wrap':true,chat:true,disabled:_parentThis.chatStatus}">
          <a href="tencent://message/?uin=3140580083&Site=sc.chinaz.com&Menu=yes">
            <img src="../assets/QQ.png" alt="" style="margin-right: 8px;"> <span style="color: #333333">在线咨询</span>
          </a>
          <span class="badge" @click="closeChat">X</span>
        </div>
        <router-link :to="{name:'cartList'}" v-if="cartNum!==0">
          <div :class="{'nav-car-wrap':true,disabled:_parentThis.cartStatus}">
            <img src="../assets/car-icon.png" alt=""> <span>购物车</span>
            <span class="badge">{{cartNum}}</span>
          </div>
        </router-link>
      </div>
    </div>
    <Notify :parentThis='this'></Notify>
  </div>
</template>

<script>
  import API from '../api'
  import Notify from '../components/Notify'
  export default {
    name: 'navCar',
    components: {
      Notify
    },
    props: ['_parentThis'],
    data: function () {
      return {
        cartNum: 0,
        goldPrice: 0,
        notifyStatus: 2
      }
    },
    methods: {
      cartList: function () {
        if (this.$cookie.get('id')) {
          this.$http.post(API.ShopCarList, {
            userId: this.$cookie.get('id')
          }).then((ret) => {
            const result = ret.body
            if (result.code === 1) {
              this.cartNum = result.object.totalElements
            } else {
              this.notifyStatus = '0|' + result.message
            }
          }, (err) => {
            console.log(err)
          })
        } else {
          this.cartStatus = true
        }
      },
      findGoldPrice: function () {
        this.$http.post(API.findGoldPrice, {
          userId: this.$cookie.get('id')
        }).then((ret) => {
          const result = ret.body
          if (result.code === 1) {
            this.goldPrice = result.object
          } else {
            this.notifyStatus = '0|' + result.message
          }
        }, (err) => {
          console.log(err)
        })
      },
      closeRealGold: function () {
        this._parentThis.realgoldStatus = !this._parentThis.realgoldStatus
      },
      closeChat: function () {
        this._parentThis.chatStatus = !this._parentThis.chatStatus
      }
    },
    updated: function () {
      this.cartList()
    },
    mounted: function () {
      this.cartList()
      this.findGoldPrice()
    }
  }
</script>
